package com.hs.srm.admin.modules.bills.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sunnykid.Sunnykid;

import com.ctc.wstx.util.StringUtil;
import com.hs.srm.admin.modules.bills.entity.ScmBillsEntity;
import com.hs.srm.admin.modules.bills.service.ScmBillsService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.common.utils.DictDataUtil;
import com.hs.srm.base.common.utils.DictDataUtil.DictType;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;

/**
 * <p>標題：对账单controller层</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:49:54
 * @since V1.0.0
 */
@RestController
@RequestMapping("/scmBills")
public class ScmBillsController extends AbstractController {

	/**
	 * 类序列ID
	 */
	private static final long serialVersionUID = 3471881119085172718L;

	@Autowired
	private ScmBillsService scmBillsService;

	/**
	 * 查询列表
	 */
	@RequestMapping("/list")
	@ResponseBody
	public R list(@RequestParam Map<String, Object> params) {
		SysUserEntity username = this.getUser();
		if(username.getDeptId()==13){
			params.put("username",username.getUsername());
		}
		PageUtils pu = this.scmBillsService.selectDetailPage(params);
		List<ScmBillsEntity> list = (List<ScmBillsEntity>)pu.getList();
		for(ScmBillsEntity bill : list) {
			BigDecimal totalPrice = null;
			BigDecimal price = bill.getPoPrice();
			BigDecimal count = bill.getPoCount();
			if(Sunnykid.isEmpty(price) || Sunnykid.isEmpty(count)) {
				continue;
			}
			totalPrice = price.multiply(count);
			bill.setTotalPrice(totalPrice);
			// BigDecimal totalPrice = bill.getTotalPrice();
			String taxCode = bill.getTaxCode();
			if(Sunnykid.isEmpty(totalPrice) || Sunnykid.isEmpty(taxCode)) {
				continue;
			}
			BigDecimal taxMoney = this.scmBillsService.calTaxMoney(totalPrice,
					DictDataUtil.valToTxt(taxCode, DictType.SDM));
			bill.setTaxMoney(taxMoney);
		}
		// List<ScmPoItemVo> voList = (List<ScmPoItemVo>)pu.getList();
		// voList = EntityUtils.valToTxt(voList);
		return R.ok(pu);
	}

	/**
	 * 计算税额
	 *
	 * @param billMonoey  对账单金额
	 * @param taxCode 税代码的key
	 */
	@RequestMapping("/calTaxMoney")
	@ResponseBody
	public R calTaxMoney(BigDecimal billMonoey, String taxCode) {
		BigDecimal taxMoney = null;
		String percentStr = null;
		String taxCodeText = DictDataUtil.valToTxt(taxCode, DictType.SDM);
		try {
			percentStr = taxCodeText.substring(taxCodeText.indexOf("-") + "-".length(), taxCodeText.indexOf("%"));
		} catch(Throwable ex) {
			return R.error("税代码表述不正确");
		}
		taxMoney = billMonoey.multiply(new BigDecimal(percentStr)).divide(new BigDecimal("100"));
		return R.ok(taxMoney);
	}

}
